package org.eclipse.football;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class PlayerList {
	private ArrayList<OffPlayer> off_players = new ArrayList<OffPlayer>();
	private ArrayList<OffPlayer> running_backs = new ArrayList<OffPlayer>();
	private ArrayList<OffPlayer> wide_outs = new ArrayList<OffPlayer>();
	private ArrayList<OffPlayer> tight_ends = new ArrayList<OffPlayer>();
	private ArrayList<DefPlayer> def_players = new ArrayList<DefPlayer>();
	private ArrayList<Kicker> kickers = new ArrayList<Kicker>();

	// Define constructor
	public PlayerList() throws IOException {
		// Set the member variables to be the value of the input variables
		getData("qbList.csv", off_players);
		getData("rbList.csv", running_backs);
		getData("wrList.csv", wide_outs);
		getData("teList.csv", tight_ends);
		getDataK("kickList.csv", kickers);
		getDataD(def_players);
	}

	public ArrayList<DefPlayer> getDef_players() {
		return def_players;
	}

	public void setDef_players(ArrayList<DefPlayer> def_players) {
		this.def_players = def_players;
	}

	public ArrayList<OffPlayer> getOff_players() {
		return off_players;
	}

	public ArrayList<OffPlayer> getRunning_backs() {
		return running_backs;
	}

	public ArrayList<OffPlayer> getWide_outs() {
		return wide_outs;
	}

	public ArrayList<OffPlayer> getTight_ends() {
		return tight_ends;
	}

	public ArrayList<Kicker> getKickers() {
		return kickers;
	}

	public void setOff_players(ArrayList<OffPlayer> off_players) {
		this.off_players = off_players;
	}

	public void setRunning_backs(ArrayList<OffPlayer> running_backs) {
		this.running_backs = running_backs;
	}

	public void setKickers(ArrayList<Kicker> kickers) {
		this.kickers = kickers;
	}

	public static void getData(String fileName, ArrayList<OffPlayer> players)
			throws IOException {
		//System.out.println(fileName);
		File inputFile = new File(fileName);
		FileReader inputFileReader = null;
		try {
			inputFileReader = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.err.println("No such file" + inputFile.getAbsolutePath());
			e.printStackTrace();
		}
		BufferedReader bufferedFileReader = new BufferedReader(inputFileReader);
		String lineOfInput = null;
		try {
			while ( bufferedFileReader.ready() && ((lineOfInput = bufferedFileReader.readLine()) != null)) {
				String[] tokens = lineOfInput.split(":");
				tokens[0] = tokens[0].replace(" ","");
				String[] name = tokens[0].split(",");
				players.add(new OffPlayer(name[1].replace("\"", "") + " "
						+ name[0].replace("\"", ""), tokens[1].replace("\"", "")));
			}
		} finally {
			bufferedFileReader.close();
			inputFileReader.close();
		}
	}

	public static void getDataK(String fileName, ArrayList<Kicker> players)
			throws IOException {
		File inputFile = new File(fileName);
		FileReader inputFileReader = null;
		try {

			inputFileReader = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.err.println("No such file" + inputFile.getAbsolutePath());
		}
		BufferedReader bufferedFileReader = new BufferedReader(inputFileReader);
		String lineOfInput = null;
		try {
			while ( bufferedFileReader.ready() && ((lineOfInput = bufferedFileReader.readLine()) != null)) {
				String[] tokens = lineOfInput.split(":");
				String[] name = tokens[0].split(",");
				players.add(new Kicker(name[1].replace("\"", "") + " "
						+ name[0].replace("\"", ""), tokens[1].replace("\"", "")));
			}
		} finally {
			bufferedFileReader.close();
			inputFileReader.close();
		}
	}
	
	public static void getDataD(ArrayList<DefPlayer> players)
			throws IOException {
		File inputFile = new File("TeamList.csv");
		FileReader inputFileReader = null;
		try {

			inputFileReader = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.err.println("No such file" + inputFile.getAbsolutePath());
		}
		BufferedReader bufferedFileReader = new BufferedReader(inputFileReader);
		String lineOfInput = null;
		try {
			while ( bufferedFileReader.ready() && ((lineOfInput = bufferedFileReader.readLine()) != null)) {
				String[] tokens = lineOfInput.split(",");
				players.add(new DefPlayer(tokens[2].replace("\"", ""), tokens[1].replace("\"", "")));
			}
		} finally {
			bufferedFileReader.close();
			inputFileReader.close();
		}
	}
	
	// Make a function to find an offensive player by their name
	public OffPlayer findOffByName(String name){
		// Initialize a null player
		OffPlayer result = null;
		
		// Iterate through offensive players array
		for(int i = 0; i < off_players.size(); i++){
			// Check if this players name matches 
		}
		// Return result
		return result;
	}
	
}
